package cn.qlu.yhy.service.aop;

import org.aopalliance.intercept.MethodInvocation;
import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;

import cn.qlu.yhy.utils.AppContext;

public class LogMethodTimeAspect {

    private final Logger logger = Logger.getLogger(LogMethodTimeAspect.class);

    public void doAfter(ProceedingJoinPoint pjp) {
        logger.info("doAfter method ：" + pjp.getTarget().getClass().getSimpleName() + " : " + pjp.getSignature().getName());
        System.out.println("doAfter method ：" + pjp.getTarget().getClass().getSimpleName() + " : " + pjp.getSignature().getName());
    }

    public Object doAround(MethodInvocation invocation) throws Throwable {

        long startTime = System.currentTimeMillis();

        Object returnValue = invocation.proceed();
        String methodName = invocation.getMethod().getName();

        long endTime = System.currentTimeMillis();

        StringBuilder sb = new StringBuilder();

        String userName = AppContext.getAppContext().getUserName();
        String className = invocation.getMethod().getDeclaringClass().getSimpleName();
        sb.append(userName + " : " + className + " : " + methodName + " : " + (endTime - startTime));

        logger.info(sb.toString());
        //        System.out.println(sb.toString());
        return returnValue;
    }
}
